package cn.com.vs.service;

import java.util.List;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import cn.com.vs.Configurator;

public class RedisService {
	
	private JedisPool pool = null;
	
	protected RedisService(){
		JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(20);
        config.setMaxIdle(10);
        config.setMaxWaitMillis(5000L);
        config.setTestOnBorrow(true);
        int port = Configurator.getInstance().getInt("redisServerPort");
        pool = new JedisPool(config, Configurator.getInstance().getString("redisServer"),port );
	}
	
	private static RedisService inst = null;
	
	public static void init(){
		inst = new RedisService();
	}
	
	public static RedisService getInstance(){
		return inst;
	}
	
	public boolean setValue(String key,String value){
		Jedis redis = null;
		try{
			redis = pool.getResource();
			redis.select(1);
			String ret = redis.set(key, value);
			if( ret.compareTo("OK") == 0 ){
				return true;
			}
			return false;
		}finally{
			if( redis != null)
				pool.returnResource(redis);
		}
	}
	
	public boolean setValue(String key,String value,int expireTime){
		Jedis redis = null;
		try{
			redis = pool.getResource();
			redis.select(1);
			if( redis.exists(key) ){
				
				String ret = redis.set(key, value, "XX", "EX", expireTime);
				if( ret.compareTo("OK") != 0 )
					return false;
				return true;
			}else{
				String ret = redis.set(key, value,"NX","EX",expireTime);
				if( ret.compareTo("OK") == 0 ){
					return true;
				}
			}
			return false;
		}finally{
			if( redis != null)
				pool.returnResource(redis);
		}
	}
	
	public String getValue(String key){
		Jedis redis = null;
		try{
			redis = pool.getResource();
			redis.select(1);
			return redis.get(key);
		}finally{
			if( redis != null)
				pool.returnResource(redis);
		}
	}
	
	public boolean delete(String key){
		Jedis redis = null;
		try{
			redis = pool.getResource();
			redis.select(1);
			Long ret = redis.del(key);
			return ret>0;
		}finally{
			if( redis != null)
				pool.returnResource(redis);
		}
	}
	
	public boolean exist(String key){
		Jedis redis = null;
		try{
			redis = pool.getResource();
			redis.select(1);
			Boolean ret = redis.exists(key);
			return ret;
		}finally{
			if( redis != null)
				pool.returnResource(redis);
		}
	}
	
	public boolean expire(String key,int timeout){
		Jedis redis = null;
		try{
			redis = pool.getResource();
			redis.select(1);
			Long l = redis.expire(key, timeout);
			return l>0;
		}finally{
			if( redis != null)
				pool.returnResource(redis);
		}
	}
	
	public boolean publishNotice(String key,String msg){
		Jedis redis = null;
		try{
			redis = pool.getResource();
			redis.select(0);
			Long ret = redis.lpush(key+"_notice", msg);
			return ret==1;
		}finally{
			if( redis != null)
				pool.returnResource(redis);
		}
	}
	
	public boolean publishNotice(List<String> keys,String msg){
		Jedis redis = null;
		try{
			redis = pool.getResource();
			redis.select(0);
			for( String k:keys ){
				String key = k+"_notice";
				Long ret = redis.rpush(key, msg);
				System.out.println(key+" messages:"+ret);
			}
			return true;
		}finally{
			if( redis != null)
				pool.returnResource(redis);
		}
	}
	
	public List<String> getNotice(String key){
		Jedis redis = null;
		try{
			redis = pool.getResource();
			redis.select(0);
			List<String> ret = redis.lrange(key+"_notice", 0, -1);
			if( ret.size() > 0 )
				redis.del(key+"_notice");
			return ret;
		}finally{
			if( redis != null)
				pool.returnResource(redis);
		}
	}
}
